// qrcode-scanning.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  onSuccess: function(reponse){
    if (reponse.code == 200 && reponse.data ){
        if(reponse['data']['storage']){
          for (var key in reponse['data']['storage']){
            if (reponse['data']['storage'][key]){
              wx.setStorage({
                key: key,
                data: reponse['data']['storage'][key],
              })
            }
          }
        }
        wx.redirectTo({
          url: reponse['data']['path'],
        })
    }else{
      wx.showToast({
        title: reponse.msg,
        icon: 'success',
        duration: 2000
      });
    }
  },
  onScan: function (options){
    var that = this
    getApp().http.request({
      url: '/api/v1/menu/info',
      data: options,
      fail: function(){
        wx.showToast({
          title: '网络异常,请重试!',
          icon: 'success',
          duration: 2000
        });
      },
      success: function (res) {
        that.onSuccess(res.data) 
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that.onScan(options)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  /**
   * 开启扫码功能
   */
  openscan: function () {
    wx.scanCode({
      success: (res) => {
        console.log(res)
        wx.showToast({
          title: '扫码成功',
          icon: 'success',
          duration: 2000,
          complete: function () {
            var obj = tool.queryToObject(res.path)
            util.http({
              url: '/api/v1/seat/scan?' + res.path,
              success: function (res) {
                var reponse = res.data;
                if (reponse.code == 200 && reponse.data['order_seat_sn']) {
                  app.globalData.order_seat_sn =
                    reponse.data['order_seat_sn']
                  wx.navigateTo({
                    url: '/pages/eating/eating?order_seat_sn=' + reponse.data['order_seat_sn']
                  })
                }
              }
            })
          }
        })
      }
    })
  }
})